var logger = require('morgan');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

server.listen(8000);

//Register the index route
app.get('/', function (req, res, next) {
    console.log('Homepage');
    res.sendFile(__dirname+'/index.html');
});

app.use(logger('common'));
app.use('/static', express.static('./node_modules'));

// Handle connection
io.on('connection', function (socket) {
    console.log("Connected succesfully to the socket ...");

    var progress = 0;
    var interval = setInterval(function () {
        // Send update the socket
        socket.emit('update', progress);
        progress+=5;
        if (progress>50){
            socket.emit('finish');
            socket.disconnect();
            clearInterval(interval);
        }
    }, 2000);

    // //listen customized event
    // socket.on('my other event', function (data) {
    //     console.log(data);
    // });
});

